<?php
/**
 * 后台班级级别 模块
 */
#定义命名空间
namespace Admin\Controller;

class ClassTypeController extends ComController {

    /**
     * 班级列表
     * @return [type] [description]
     */
    public function index(){
        //获取当前登录用户所属的学校id
        $sc_id = session('sc_id');
        $class = D('class_type');
        $where['sc_id'] = $sc_id;
        $where['is_del'] = 1; //未删除
        $prefix = C('DB_PREFIX');//读取配置文件 数据表前缀
        $p = I('get.p') ? I('get.p') : 1;
        $pageSize = 15; //每页显示条数
        $limit = ($p-1)*$pageSize; //计算偏移量
        $order = ' id desc';
        $count = $class->where($where)->count();
        $class_info = $class->where($where)->order($order)->limit($limit.','.$pageSize)->select();
        $page = new \Think\Page($count,$pageSize);
        $page = $page->show();
        $this->assign('page',$page);
        $this->assign('class',$class_info);
        $this->display();
    }

    /**
     * 添加班级信息
     */
    public function append(){
        //获取当前登录用户所属的学校id
        $sc_id = session('sc_id');
        if(IS_POST){
            //接收参数 验证
            $class = D('class_type');
            $param = I('post.');
            $param['sc_id'] = $sc_id;
            $param['add_time'] = time();
            $res = $class->create($param);
            if($res){
                //检测名称是否已被使用
                $where['sc_id'] = $sc_id;
                $where['name'] = I('post.name');
                $isNameSet = $class->where($where)->find();
                if(!$isNameSet){

                    $class_res = $class->add($param);
                    if($class_res){

                        $this->success('添加成功!',U('index'));
                    }else{
                        $this->error('添加失败!');
                    }
                }else{
                    $this->error('级别名称已经存在，不能重复添加!');
                }
            }else{
                $this->error($class->getError());
            }
        }else{
            //展示页面
            $member = M('member');//实例化教师模型
            $where['pid'] = $sc_id;
            $where['level'] = 2;
            $member_info = $member->where($where)->field('uid,name,teacher_type,class_id')->select();
            $this->assign('teacher',$member_info);
            $this->assign('form_url',U('append'));
            $this->display();
        }
    }

    /**
     * 修改班级信息
     * @return boolean 成功返回true 失败返回false
     */
    public function edit(){
        //获取当前登录用户所属的学校id
        $sc_id = session('sc_id');
        $class = D('class_type');//实例化模型
        $id = I('get.id') ? I('get.id') : 0 ;
        
        if(IS_POST){
            //接收参数 验证
            $param = I('post.');
            $param['update_time'] = time();
            $res = $class->create($param);
            if($res){
                $where['id'] = $param['id'];
                $name = $param['name'];
                $c_where['sc_id'] = $sc_id;
                $c_where['name'] = $name;
                $data = $class->where('id = '.$param['id'])->find();//通过ID查询信息判断名称是否修改
                unset($param['id']);
                if($data['name'] != $name){

                    //检测用户名是否存在
                    $class_data = $class->where($c_where)->find();
                    if($class_data){
                        $this->error('级别名称已经存在，不能重复添加');
                    }else{

                        $class_res = $class->where($where)->save($param);
                        if($class_res !== false ){

                            $this->success('修改成功!',U('index'));
                        }else{
                            $this->error('修改失败!',U('index'));
                        }
                    }
                }else{
                    $this->success('修改成功!',U('index'));
                }
            }else{
                $this->error($class->getError());
            }
        }else{
            //展示页面
            if($id){
                $where['id'] = $id;
                $class_info = $class->where($where)->find();
                $this->assign('class',$class_info);
                $this->assign('form_url',U('edit'));
                $this->display('append');
            }else{
                $this->error('缺少参数!',U('index'));
            }
        }
    }

    /**
     * 删除班级
     * @return  boolean 成功返回true 失败返回false
     */
    public function del(){
        $id = I('get.id') ? I('get.id') : 0 ;//获取主键id   
        if($id){
            $class = D('class_type');
            $where['id'] = $id;
            $data = $class->where($where)->find();
            //检验是否可以删除
            if($this->is_del($id)){

                $res = $class->where($where)->delete();
                if($data && $res){
                    $this->error('删除成功!',U('index'));
                }else{
                    $this->error('删除失败!',U('index'));
                }
            }else{
                $this->error('该级别下有关联数据，不能删除!');
            }
        }else{
            $this->error('缺少参数!');
        }
    }

    /**
     * ajax根据班级级别 获取教师信息
     * http post请求
     */
    public function getTeacher(){

        $class_id = I('post.class_id') ? I('post.class_id') : 3; //获取参数
        if($class_id){
            //获取当前登录用户的学校id
            $sc_id = session('sc_id');
            $where['pid'] = $sc_id;
            $where['class_id'] = $class_id;
            $where['level'] = 2;
            $member = M('member');//实例化教师|幼儿园表
            $teacher = $member->where($where)->field('uid,name,class_id,teacher_type')->select();
            if($teacher){
                echo json_encode(array('res'=>1,'data'=>$teacher,'msg'=>'查询成功'));
            }else{
                echo json_encode(array('res'=>0,'data'=>'','msg'=>'查询失败，没有数据'));
            }
        }else{
            echo json_encode(array('res'=>0,'data'=>'','msg'=>'查询失败，缺少参数'));
        }
    }

    /**
     * 根据班级级别id 查看是否有关联的信息
     * 如有则不能删除 否则 可以删除
     * @return boolean 返回true则可以删除 返回false 不能删除
     */
    public function is_del($id=0){
        $sc_id = session('sc_id');
        $class = M('class');
        $member = M('member');
        $c_where['sc_id'] = $sc_id;
        $c_where['class_type'] = $id;
        $m_where['pid'] = $sc_id;
        $m_where['class_id'] = $id;
        $c_res = $class->where($c_where)->select();
        $m_res = $member->where($m_where)->select();
        if($c_res || $m_res){
            return false;
        }else{
            return true;
        }
    }
}